<template>
  <div class="mobile-container">
    <div class="app-content">
      <!-- 顶部栏 -->
      <div class="header">
        <div class="header-top">
          <div class="back-icon" @click="handleBack">
            <Icon icon="mdi:arrow-left"/>
          </div>
          <div class="page-title">转到置换宝数量</div>
          <div class="placeholder"></div>
        </div>
      </div>

      <!-- 主要内容 -->
      <div class="content">
        <!-- 转出数量输入 -->
        <div class="input-section">
          <div class="input-label">转到置换宝数量(实际到账)</div>
          <div class="input-group">
            <input
              v-model="transferAmount"
              type="number"
              class="amount-input"
              placeholder="0"
              @input="calculateDeduction"
            >
          </div>
          <div class="balance-info">
            账户余额{{ countAmount }}个,可转换数量{{ countAmount }}个 <span class="all-transfer" @click="transferAll">全部转出</span>
          </div>
        </div>

        <!-- 损耗信息 -->
        <div class="loss-section">
          <div class="loss-info">
            <span class="loss-label">损耗</span>
            <span class="loss-value">{{ lossRate }}%</span>
          </div>
          <div class="loss-info">
            <span class="loss-label">实际扣除数量</span>
            <span class="loss-value">{{ actualDeduction }}</span>
          </div>
        </div>

        <!-- 验证码输入 -->
        <div class="captcha-section">
          <div class="input-label">请输入验证码</div>
          <div class="captcha-group">
            <input
              v-model="captcha"
              type="text"
              class="captcha-input"
              placeholder="请输入验证码"
              maxlength="6"
            >
            <button class="get-captcha-btn" @click="getCaptcha">
              {{ countdown > 0 ? `${countdown}秒后重新获取` : '获取验证码' }}
            </button>
          </div>
        </div>

        <!-- 二级密码输入 -->
        <div class="password-section">
          <div class="input-label">请输入二级密码</div>
          <div class="password-input-wrapper">
            <input
              v-model="secondaryPassword"
              :type="showPassword ? 'text' : 'password'"
              class="password-input"
              placeholder="请输入二级密码"
            >
            <div class="password-toggle" @click="showPassword = !showPassword">
              <Icon :icon="showPassword ? 'mdi:eye-off' : 'mdi:eye'" class="toggle-icon"/>
            </div>
          </div>
        </div>

        <!-- 确认按钮 -->
        <div class="action-section">
          <button
            class="confirm-btn"
            :class="{ disabled: !canTransfer }"
            :disabled="!canTransfer"
            @click="handleTransfer"
          >
            确认转出
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { Icon } from '@iconify/vue2'

export default {
  name: 'TransferToTreasurePage',
  components: {
    Icon
  },
  data() {
    return {
      countAmount: 100,
      transferAmount: '',
      lossRate: 7, // 损耗率，可配置
      actualDeduction: 0,
      captcha: '',
      secondaryPassword: '',
      showPassword: false,
      countdown: 0,
      countdownInterval: null
    }
  },
  computed: {
    canTransfer() {
      return this.transferAmount > 0 &&
        this.captcha.length >= 4 &&
        this.secondaryPassword.length >= 6
    }
  },
  beforeDestroy() {
    if (this.countdownInterval) {
      clearInterval(this.countdownInterval)
    }
  },
  methods: {
    handleBack() {
      this.$router.go(-1)
    },

    calculateDeduction() {
      if (!this.transferAmount || this.transferAmount <= 0) {
        this.actualDeduction = 0
        return
      }

      // 计算实际扣除数量：转出数量 + 转出数量 * 损耗百分比
      const amount = parseFloat(this.transferAmount)
      const lossAmount = amount * (this.lossRate / 100)
      const totalDeduction = amount + lossAmount

      // 四舍五入保留4位小数
      this.actualDeduction = Math.round(totalDeduction * 10000) / 10000
    },

    transferAll() {
      // 模拟获取全部可转出数量
      this.transferAmount = this.countAmount
      this.calculateDeduction()
    },

    getCaptcha() {
      if (this.countdown > 0) return

      // 模拟发送验证码请求
      console.log('获取验证码')

      // 开始倒计时
      this.countdown = 60
      this.countdownInterval = setInterval(() => {
        this.countdown--
        if (this.countdown <= 0) {
          clearInterval(this.countdownInterval)
        }
      }, 1000)
    },

    handleTransfer() {
      if (!this.canTransfer) return

      console.log('转出申请提交:', {
        transferAmount: this.transferAmount,
        actualDeduction: this.actualDeduction,
        captcha: this.captcha,
        secondaryPassword: this.secondaryPassword
      })

      // 模拟提交转出申请
      alert('转出申请已提交，等待处理')

      // 重置表单
      this.resetForm()
    },

    resetForm() {
      this.transferAmount = ''
      this.actualDeduction = 0
      this.captcha = ''
      this.secondaryPassword = ''
    }
  }
}
</script>

<style scoped>
.mobile-container {
  width: 100%;
  min-height: 100vh;
  background: #f5f5f5;
  display: flex;
  justify-content: flex-start;
  align-items: flex-start;
  padding: 20px;
}

.app-content {
  width: 375px;
  min-height: 667px;
  background: white;
  border-radius: 20px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  position: relative;
  display: flex;
  flex-direction: column;
}

.header {
  padding: 15px 20px;
  background: white;
  border-bottom: 1px solid #f0f0f0;
}

.header-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.back-icon {
  padding: 8px;
  border-radius: 8px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  color: #333;
}

.page-title {
  font-size: 16px;
  font-weight: 600;
  color: #333;
  text-align: center;
  flex: 1;
}

.placeholder {
  width: 36px;
}

.content {
  flex: 1;
  padding: 20px;
  display: flex;
  flex-direction: column;
}

.input-section {
  margin-bottom: 30px;
}

.input-label {
  font-size: 14px;
  color: #666;
  margin-bottom: 8px;
}

.input-group {
  position: relative;
  display: flex;
  align-items: center;
  margin-bottom: 12px;
}

.amount-input {
  flex: 1;
  height: 50px;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  padding: 0 16px;
  font-size: 18px;
  font-weight: 500;
  outline: none;
  transition: all 0.3s ease;
}

.amount-input:focus {
  border-color: #667eea;
}

.input-unit {
  position: absolute;
  right: 16px;
  font-size: 14px;
  color: #999;
}

.balance-info {
  font-size: 14px;
  color: #999;
}

.all-transfer {
  color: #667eea;
  margin-left: 8px;
  cursor: pointer;
}

.all-transfer:hover {
  text-decoration: underline;
}

.loss-section {
  background: #f8f9fa;
  border-radius: 8px;
  padding: 16px;
  margin-bottom: 30px;
}

.loss-info {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.loss-info:last-child {
  margin-bottom: 0;
}

.loss-label {
  font-size: 14px;
  color: #666;
}

.loss-value {
  font-size: 14px;
  color: #333;
  font-weight: 500;
}

.captcha-section {
  margin-bottom: 30px;
}

.captcha-group {
  display: flex;
  gap: 12px;
}

.captcha-input {
  flex: 1;
  height: 44px;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  padding: 0 12px;
  font-size: 14px;
  outline: none;
  transition: all 0.3s ease;
}

.captcha-input:focus {
  border-color: #667eea;
}

.get-captcha-btn {
  padding: 0 16px;
  background: #667eea;
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.3s ease;
  white-space: nowrap;
  min-width: 120px;
}

.get-captcha-btn:hover:not(:disabled) {
  background: #5a6fd8;
}

.get-captcha-btn:disabled {
  background: #ccc;
  cursor: not-allowed;
}

.password-section {
  margin-bottom: 40px;
}

.password-input-wrapper {
  position: relative;
  display: flex;
  align-items: center;
}

.password-input {
  width: 100%;
  height: 44px;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  padding: 0 12px;
  font-size: 14px;
  outline: none;
  transition: all 0.3s ease;
  padding-right: 40px;
}

.password-input:focus {
  border-color: #667eea;
}

.password-toggle {
  position: absolute;
  right: 12px;
  color: #999;
  cursor: pointer;
  padding: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.toggle-icon {
  font-size: 18px;
  transition: all 0.3s ease;
}

.password-toggle:hover .toggle-icon {
  color: #667eea;
}

.action-section {
  margin-top: auto;
}

.confirm-btn {
  width: 100%;
  height: 50px;
  background: #667eea;
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
}

.confirm-btn:hover:not(.disabled) {
  background: #5a6fd8;
}

.confirm-btn.disabled {
  background: #ccc;
  cursor: not-allowed;
}

/* 响应式设计 */
@media (max-width: 375px) {
  .mobile-container {
    padding: 0;
  }

  .app-content {
    width: 100%;
    min-height: 100vh;
    border-radius: 0;
  }
}
</style>
